import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:miuu_note_app/dao/snippet_dao.dart';
import 'package:miuu_note_app/page/snippet_add_update.dart';

import '../constant/todo_enum.dart';
import '../model/snippet_entity.dart';

class SnippetDetail extends StatefulWidget {
  const SnippetDetail({super.key});

  @override
  State<SnippetDetail> createState() => _SnippetDetailState();
}

class _SnippetDetailState extends State<SnippetDetail> {
  SnippetModel? _snippetModel;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      dynamic arguments = ModalRoute.of(context)?.settings.arguments;
      if (arguments != null) {
        _getSnippetDetail(arguments['snippetId']);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: BrnAppBar(
              leading: BrnBackLeading(
                iconPressed: () => Navigator.pop(context),
              ),
              //默认显示返回按钮
              automaticallyImplyLeading: true,
              title: '随便记记',
            ),
            body: SingleChildScrollView(
                child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                  BrnNoticeBarWithButton(
                    leftTagText: SnippetType.getLabelByName(
                            _snippetModel?.type ?? 'work') ??
                        '',
                    leftTagBackgroundColor: const Color(0xFFE0EDFF),
                    leftTagTextColor: const Color(0xFF0984F9),
                    content: _snippetModel?.intro ?? '本文主要记录了以下内容，欢迎收藏.',
                    backgroundColor: const Color(0xFFEBFFF7),
                    contentTextColor: const Color(0xFF00AE66),
                    rightButtonBorderColor: const Color(0xFF0984F9),
                    rightButtonTextColor: const Color(0xFF0984F9),
                    rightButtonText: '编辑',
                    onRightButtonTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => const SnippetAdd(),
                            settings: RouteSettings(arguments: {
                              "title": "编辑",
                              "snippet": _snippetModel!.toJson()
                            }) // 需要跳转的页面
                            ),
                      ).then((value) {
                        if (value != null) {
                          _getSnippetDetail(int.parse(value.data));
                        }
                      });
                    },
                  ),
                  Padding(
                    padding: const EdgeInsets.only(
                        left: 25, right: 25, top: 25, bottom: 20),
                    child: Text(_snippetModel?.title ?? '暂无标题',
                        textAlign: TextAlign.left,
                        style: const TextStyle(
                            fontSize: 20, fontWeight: FontWeight.w600)),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(
                        left: 25, right: 25, top: 25, bottom: 20),
                    child: Text(_snippetModel?.intro ?? '',
                        textAlign: TextAlign.left,
                        style: const TextStyle(
                            fontSize: 20, fontWeight: FontWeight.w600)),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(
                        left: 25, right: 25, top: 0, bottom: 20),
                    child: Text(_snippetModel?.content ?? '',
                        textAlign: TextAlign.justify,
                        style: const TextStyle(fontSize: 14)),
                  ),
                ]))));
  }

  void _getSnippetDetail(int snippetId) async {
    SnippetEntity? entity = await SnippetDetailDao.fetch(snippetId);
    setState(() {
      _snippetModel = entity?.snippetModel;
    });
  }
}
